<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>机构选择</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../../static/lib/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../../static/lib/layuiadmin/style/admin.css" media="all">

    <style>
        html,body{height: 100%;}
        .layui-fluid{height: 96%;}
        .layui-row,.layui-card,.layui-col-xs12{height: 100%;}
    </style>
</head>
<body>

<div class="layui-fluid" id="LAY-component-grid-mobile">
    <div class="layui-row layui-col-space10">
        <!--<div class="" style="margin-bottom: 10px; background-color: white; height: 30px; margin-left: 6px; margin-right: 6px" >-->
            <!--<form class="layui-form" action="">-->
                <!--<table style="width:98%; table-layout:fixed;">-->
                    <!--<tr>-->
                        <!--<td style='width:80px'>-->
                            <!--<a id="save" class="layui-btn layui-btn-sm st_back1"  onclick="save()" > 确  定</a>-->
                        <!--</td>-->
                    <!--</tr>-->
                <!--</table>-->
            <!--</form>-->
        <!--</div>-->
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header">机构</div>
                <div class="layui-card-body">
                    <ul id="Tree"></ul>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script src="../../../../static/lib/layuiadmin/layui/layui.js"></script>
<script src="../../../common/common.js"></script>
<script src="../../../common/jquery-3.3.1.min.js"></script>
<script src="../../../common/listTree.js"></script>
<script src="../../../common/dataGrid.js"></script>
<script>

    UserInfo.init();
    curpsnid = UserInfo.userId; //当前操作者id
    cur_psn = UserInfo.userName; //当前操作者
    cur_orgid= UserInfo.orgId;
    cur_orgname= UserInfo.orgName;

    var orgMap = new Map();
    var currentorgId, currentorgName, currentTreeNode;
    $(document).ready(function () {
        layui.use(['form', 'laydate', 'table', 'upload'], function () {
            form = layui.form;
            laydate = layui.laydate;

            loadOrgTree();
        });
    });

    //加载机构树
    function loadOrgTree(){
        $("#Tree").html("");
        //listTree.load("Tree",{"sysOrgVO.orgState" : "1","sysOrgVO.orgPathLike":cur_orgid});
        layui.use('tree', function(){
            $.post("/sys-org/orgList", { }, function (data) {
                if (data.code == 0) {
                    for (var i = 0; i < data.List.length; i++) {
                        orgMap.set(data.List[i].id, data.List[i]);
                    }

                    layui.tree({
                        elem: '#Tree' //传入元素选择器
                        , nodes: formatData(data.List, "orgPid", "id", "orgName")
                        , click: function (node) {
                            treeselected(node);
                        }
                    });
                } else {
                    layer.msg('取得模块数据失败！', {
                        icon: 2,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {})
                }
            });

        });
    }

    //机构树点击事件
    function treeselected(node){
        currentorgId=node.id;
        currentorgName=node.name;
        currentTreeNode=node.node;

        var index = parent.layer.getFrameIndex(window.name);
        var iframe = RequestURLParam.getParam("iframeId");
        pnode = $('#'+iframe,parent.document)[0].contentWindow.node;

        pnode.val(node.name);
        pnode.attr("value", node.name);
        pnode.attr("value-code", node.id);

        parent.layer.close(index);
    }


    /**
     * 数据封装
     * @param data 数据
     * @param pid  父ID
     * @param id  ID
     * @param name 显示名称
     * @param checked 选中状态
     */
    function formatData(values, pid, id, name){
        var tree = [],
            item;

        var map = new Map();

        // 1)变格式保存到Map
        for (var i = 0; i < values.length; i++) {
            item = new Object();

            item.name = values[i][name];
            item.id = values[i][id];
            item.pid = values[i][id] ? 0 : values[i][id];
            item.value = values[i];
            item.spread = true;
            item.children = [];

            map.set(values[i][id], item);
        }

        // 2)作为子项目加到父项目的list
        for (var i = 0; i < values.length; i++) {
            if (values[i][pid] > 0){
                var obj = map.get(values[i][pid]);
                obj.children.push(map.get(values[i][id]));
            } else {
                tree.push(map.get(values[i][id]));
            }
        }

        // 3)删除没有子项目的list
        for (var i = 0; i < values.length; i++) {
            var obj = map.get(values[i][id]);
            if (obj.children.length == 0) {
                delete obj.children;
            }
        }

        return tree;
    }

</script>